﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;


namespace Pongster
{
    public class OptionsScreen : BaseGameState
    {
        PictureBox backgroundImage;
        LinkLabel backGame;
        LinkLabel keyBindings;
        LinkLabel applyChanges;
        float maxItemWidth = 0f;

        LeftRightSelector musicVolume;
        LeftRightSelector soundVolume;
        LeftRightSelector resolutionSelector;
        LeftRightSelector screenSelector;

        string[] soundItems = { "Mute", "||", "||||", "||||||", "||||||||", "||||||||||", "||||||||||||", "||||||||||||||", "||||||||||||||||", "||||||||||||||||||", "||||||||||||||||||||" };
        string[] musicItems = { "Mute", "||", "||||", "||||||", "||||||||", "||||||||||", "||||||||||||", "||||||||||||||", "||||||||||||||||", "||||||||||||||||||", "||||||||||||||||||||" };

        //string[] musicItems = { "Mute", "|", "||", "|||", "||||", "|||||", "||||||", "|||||||", "||||||||", "|||||||||", "||||||||||" };
        //string[] soundItems = { "Mute", "|", "||", "|||", "||||", "|||||", "||||||", "|||||||", "||||||||", "|||||||||", "||||||||||" };
        
        string[] resolutionItems;
        string[] screenItems = { "Windowed", "Full"};

        public OptionsScreen(Game game, GameStateManager manager)
            : base(game, manager)
        {
        }

        public override void Initialize()
        {

            base.Initialize();

            resolutionSelector.SelectedIndex = Settings.screenIndex;
            screenSelector.SelectedIndex = Settings.screenModeIndex;
            Settings.resetResValues();

        }

        protected override void LoadContent()
        {
            base.LoadContent();

            ContentManager Content = Game.Content;

            menuFont = Content.Load<SpriteFont>(@"Fonts\MenuFont");
            ControlManager = new ControlManager(menuFont, GameRef);

            backgroundImage = new PictureBox(Content.Load<Texture2D>(@"Backgrounds\optionsmenubg"), GameRef.BaseRectangle);
            ControlManager.Add(backgroundImage);

            
            Texture2D leftTexture = Content.Load<Texture2D>(@"GUI\leftarrow");
            Texture2D rightTexture = Content.Load<Texture2D>(@"GUI\rightarrow");
            Texture2D stopTexture = Content.Load<Texture2D>(@"GUI\stoparrow");

            //string res = (string)(GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width.ToString() + "x" + GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height.ToString());
            resolutionItems = new string[] { "Native", "1024x768", "1280x720", "1366x768", "1280x960", "1600x900", "1920x1080" };

            //Labels

            Label optionsLabel = new Label();

            optionsLabel.Text = "Options Menu";
            optionsLabel.Size = optionsLabel.SpriteFont.MeasureString(optionsLabel.Text);
            optionsLabel.Position = new Vector2(Settings.basescreenWidth / 2, 85);
            optionsLabel.Origin = new Vector2(optionsLabel.Size.X / 2, 0);
            optionsLabel.Scale = 1.1f;
            ControlManager.Add(optionsLabel);

            Label musicLabel = new Label();

            musicLabel.Text = "Music Volume:";
            musicLabel.Size = musicLabel.SpriteFont.MeasureString(musicLabel.Text);
            musicLabel.Position = new Vector2((optionsLabel.Position.X - musicLabel.Size.X / 2), Settings.basescreenHeight / 2);
            ControlManager.Add(musicLabel);

            Label soundLabel = new Label();

            soundLabel.Text = "Sound Volume:";
            soundLabel.Size = soundLabel.SpriteFont.MeasureString(soundLabel.Text);
            soundLabel.Position = new Vector2((optionsLabel.Position.X - soundLabel.Size.X / 2), musicLabel.Position.Y + 50);
            ControlManager.Add(soundLabel);

            Label resLabel = new Label();

            resLabel.Text = "Resolution:";
            resLabel.Size = resLabel.SpriteFont.MeasureString(resLabel.Text);
            resLabel.Position = new Vector2((optionsLabel.Position.X - resLabel.Size.X / 2), musicLabel.Position.Y + 100);
            ControlManager.Add(resLabel);

            Label screenLabel = new Label();

            screenLabel.Text = "Screen Mode:";
            screenLabel.Size = resLabel.SpriteFont.MeasureString(screenLabel.Text);
            screenLabel.Position = new Vector2((optionsLabel.Position.X - screenLabel.Size.X / 2), musicLabel.Position.Y + 150);
            ControlManager.Add(screenLabel);

            
            //Selectors

            musicVolume = new LeftRightSelector(leftTexture, rightTexture, stopTexture, false);
            musicVolume.SetItems(musicItems, 250);
            musicVolume.Position = new Vector2(optionsLabel.Position.X + 70, musicLabel.Position.Y);
            musicVolume.SelectionChanged += new EventHandler(musicChanged);
            musicVolume.SelectedIndex = (int)(Settings.musicVolume * 10);

            ControlManager.Add(musicVolume);

            soundVolume = new LeftRightSelector(leftTexture, rightTexture, stopTexture, false);
            soundVolume.SetItems(soundItems, 250);
            soundVolume.Position = new Vector2(musicVolume.Position.X, musicVolume.Position.Y + 55);
            soundVolume.SelectionChanged += new EventHandler(soundChanged);
            soundVolume.SelectedIndex = (int)(Settings.soundVolume * 10);

            ControlManager.Add(soundVolume);

            resolutionSelector = new LeftRightSelector(leftTexture, rightTexture, stopTexture, false);
            resolutionSelector.SetItems(resolutionItems, 250);
            resolutionSelector.Position = new Vector2(musicVolume.Position.X, musicVolume.Position.Y + 113);
            resolutionSelector.SelectionChanged += new EventHandler(resolutionChanged);
            resolutionSelector.SelectedIndex = Settings.screenIndex;

            ControlManager.Add(resolutionSelector);

            screenSelector = new LeftRightSelector(leftTexture, rightTexture, stopTexture, false);
            screenSelector.SetItems(screenItems, 250);
            screenSelector.Position = new Vector2(musicVolume.Position.X, musicVolume.Position.Y + 170);
            screenSelector.SelectionChanged += new EventHandler(screenChanged);
            screenSelector.SelectedIndex = Settings.screenModeIndex;

            ControlManager.Add(screenSelector);

            //Links

            keyBindings = new LinkLabel();
            keyBindings.Text = "Change Controls";
            keyBindings.Size = keyBindings.SpriteFont.MeasureString(keyBindings.Text);
            keyBindings.Position = new Vector2(Settings.basescreenWidth / 2, musicVolume.Position.Y + 225);
            keyBindings.Origin = new Vector2(keyBindings.Size.X / 2, 0);
            keyBindings.selectedColor = Color.Gold;
            keyBindings.Selected += menuItem_Selected;

            ControlManager.Add(keyBindings);

            applyChanges = new LinkLabel();
            applyChanges.Text = "Apply Changes";
            applyChanges.Size = applyChanges.SpriteFont.MeasureString(applyChanges.Text);
            applyChanges.Position = new Vector2(Settings.basescreenWidth / 2, musicVolume.Position.Y + 280);
            applyChanges.Origin = new Vector2(applyChanges.Size.X / 2, 0);
            applyChanges.selectedColor = Color.Crimson;
            applyChanges.Selected += menuItem_Selected;

            ControlManager.Add(applyChanges);

            backGame = new LinkLabel();
            backGame.Text = "Back";
            backGame.Size = backGame.SpriteFont.MeasureString(backGame.Text);
            backGame.Position = new Vector2(Settings.basescreenWidth / 2, musicVolume.Position.Y + 355);
            backGame.Origin = new Vector2(backGame.Size.X / 2, 0);
            backGame.Selected += menuItem_Selected;

            ControlManager.Add(backGame);


            ControlManager.NextControl();
            
            //Label Positions
            foreach (Control c in ControlManager)
            {
                if (c is Label && c != optionsLabel)
                {
                    if (c.Size.X > maxItemWidth)
                        maxItemWidth = c.Size.X;
                }
                 c.Color = Color.Ivory;                
            }
            
            Vector2 position = new Vector2(Settings.basescreenWidth / 2 - maxItemWidth + 15, Settings.basescreenHeight / 2f);

            foreach (Control c in ControlManager)
            {
                if (c is Label && c != optionsLabel)
                {
                    c.Position = position;
                    position.Y += c.Size.Y + 21f;
                }
            }
            
        }
        
        private void menuItem_Selected(object sender, EventArgs e)
        {
            InputHandler.FlushInput();
            GameRef.SoundManager.PlaySFX("select", 0.8f, 0, 0);

            if (sender == backGame)
            {
                Settings.SaveSettingsFile();
                //StateManager.ChangeState(GameRef.StartMenuScreen);
                Transition(ChangeType.Change, GameRef.StartMenuScreen);

            }

            if (sender == applyChanges)
            {
                Settings.screenWidth = Settings.resWidth;
                Settings.screenHeight = Settings.resHeight;
                Settings.screenMode = Settings.resMode;
                Settings.screenModeIndex = Settings.resModeIndex;
                Settings.screenIndex = Settings.resIndex;

                Resolution.SetResolution(Settings.screenWidth, Settings.screenHeight, Settings.screenMode);                
                Settings.applyAspectRatio();

                Settings.SaveSettingsFile();
            }

            //if (sender == keyBindings)
            //{
            //    StateManager.PushState(GameRef.KeyBindingsScreen);
            //}
        }
        
        void musicChanged(object sender, EventArgs e)
        {
            GameRef.SoundManager.PlayInstantSFX("select", 0.8f, 0, 0);
            
            Settings.musicVolume = (float)musicVolume.SelectedIndex / 10;

            if (Settings.musicVolume == 0f)
                MediaPlayer.IsMuted = true;
            else
                MediaPlayer.IsMuted = false;
        }

        void soundChanged(object sender, EventArgs e)
        {
            GameRef.SoundManager.PlayInstantSFX("select", 0.8f, 0, 0);

            Settings.soundVolume = (float)soundVolume.SelectedIndex / 10;
        }

        void resolutionChanged(object sender, EventArgs e)
        {
            GameRef.SoundManager.PlayInstantSFX("select", 0.8f, 0, 0);

            if (resolutionSelector.SelectedIndex == 0)
            {
                Settings.resWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
                Settings.resHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
                Settings.resIndex = 0;
            }
            else if (resolutionSelector.SelectedIndex == 1)
            {
                Settings.resWidth = 1024;
                Settings.resHeight = 768;
                Settings.resIndex = 1;
            }
            else if (resolutionSelector.SelectedIndex == 2)
            {
                Settings.resWidth = 1280;
                Settings.resHeight = 720;
                Settings.resIndex = 2;
            }
            else if (resolutionSelector.SelectedIndex == 3)
            {
                Settings.resWidth = 1366;
                Settings.resHeight = 768;
                Settings.resIndex = 3;
            }
            else if (resolutionSelector.SelectedIndex == 4)
            {
                Settings.resWidth = 1280;
                Settings.resHeight = 960;
                Settings.resIndex = 4;
            }
            else if (resolutionSelector.SelectedIndex == 5)
            {
                Settings.resWidth = 1600;
                Settings.resHeight = 900;
                Settings.resIndex = 5;
            }
            else if (resolutionSelector.SelectedIndex == 6)
            {
                Settings.resWidth = 1920;
                Settings.resHeight = 1080;
                Settings.resIndex = 6;
            }
                       
        }

        void screenChanged(object sender, EventArgs e)
        {
            GameRef.SoundManager.PlayInstantSFX("select", 0.8f, 0, 0);

            if (screenSelector.SelectedIndex == 0)
            {
                Settings.resMode = false;
                Settings.resModeIndex = 0;
            }
            else if (screenSelector.SelectedIndex == 1)
            {
                Settings.resMode = true;
                Settings.resModeIndex = 1;
            }
        }

        public override void Update(GameTime gameTime)
        {
            ControlManager.Update(gameTime);

            if (InputHandler.KeyReleased(Keys.Escape))
            {
                Settings.SaveSettingsFile();
                StateManager.ChangeState(GameRef.StartMenuScreen);
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GameRef.SpriteBatch.Begin();
            
            ControlManager.Draw(GameRef.SpriteBatch);

            base.Draw(gameTime);

            GameRef.SpriteBatch.End();
        }

    }
}
